package com.hyg.lib_base.base;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.util.Log;


import com.hyg.lib_base.Net.BrokenNetReceiver;
import com.umeng.commonsdk.UMConfigure;

import org.litepal.LitePal;

/**
 * Created by mingjun on 16/8/25.
 */
public class InitializeService extends IntentService {

    public BrokenNetReceiver brokenNetReceiver;
    public IntentFilter filter;
    private static final String TAG = "InitializeService";
    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.anly.githubapp.service.action.INIT";

    public InitializeService() {
        super("InitializeService");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
            }
        }
    }

    private void performInit() {
        /**
         * 1、首先创建了一个IntentFilter实例,并给它添加了一个值为android.net.conn.CONNECTIVITY_CHANGE的action
         * 2、当网络发生变化时,系统发出的就是"android.net.conn.CONNECTIVITY_CHANGE"的广播
         * 3、即我们的广播接收器里面想要监听什么广播,就在这里添加相应的action
         * 4、接着创建一个NetworkChangeReceiver的实例,然后调用registerReceiver方法进行注册,将networkChangeReceiver、networkChangeReceiver都传入
         * 5、这样NetworkChangeReceiver就会收到值为"android.net.conn.CONNECTIVITY_CHANGE"的广播
         * 6、重要:动态注册的广播接收器一定要取消注册才行,调用unregisterReceiver()方法进行取消
         */
        filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //在网络连接中出现的一个变化。已被建立或断开的一个连接。
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter.addAction("android.Net.wifi.WIFI_STATE_CHANGED");
        filter.addAction("android.net.wifi.STATE_CHANGE");
        brokenNetReceiver = new BrokenNetReceiver();
        registerReceiver(brokenNetReceiver, filter);

        //友盟init
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_BOX, null);
        UMConfigure.setLogEnabled(true);

        // 加载子模块的 自定义Application
        modulesApplicationInit();
    }

    //通过反射方法找到对应的类，并调用接口
    private void modulesApplicationInit() {
        for (String moduleImpl : ModelConfig.MODULESLIST) {
            try {
                Class<?> clazz = Class.forName(moduleImpl);
                Object object = clazz.newInstance();
                Log.d(TAG, "clazz.newInstance");

                if (object instanceof ApplicationImpl) {
                    ((ApplicationImpl) object).onCreate(MyBaseApplication.getInstance());
                    Log.d(TAG, "Class.onCreate");
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }
}
